<template>
  <div>
    <div>
      <el-input
          size="small"
          v-model="jobLevel.name"
          style="width: 200px;margin-right: 8px"
          prefix-icon="el-icon-plus"
          placeholder="添加职称名称..."
          @keydown.enter.native="handleAdd">
      </el-input>
      <el-select style="margin-right: 8px" size="small" v-model="jobLevel.titleLevel" placeholder="请选择职称等级">
        <el-option
            v-for="item in titleLevel"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button type="primary" size="small" @click="handleAdd">添加</el-button>
    </div>
    <div style="margin-top: 10px">
      <el-table
          border
          size="small"
          :data="jobLevels"
          tooltip-effect="dark"
          style="width: 70%"
          @selection-change="handleSelectionChange"
      >
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="titleLevel"
            label="职称等级"
            width="120">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间"
            width="200"
        >
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否启用"
            width="200"
        >
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.enabled">已启用</el-tag>
            <el-tag type="danger" v-else>禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showDialogView(scope.$index, scope.row)">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)">删除</el-button>
          </template>

        </el-table-column>

      </el-table>
      <el-button type="danger" size="small" style="margin-top: 10px" :disabled="multipleSelection.length == 0" @click="handleBatchDelete">批量删除</el-button>
    </div>
    <el-dialog
        title="编辑职称"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <div>
          <el-tag>职称名称</el-tag>
          <el-input size="small" v-model="updateJobLevel.name" class="updatePosInput"></el-input>
        </div>
        <div>
          <el-tag>职称等级</el-tag>
          <el-select style="margin-right: 8px" size="small" v-model="updateJobLevel.titleLevel" placeholder="请选择职称等级"  class="updatePosInput">
            <el-option
                v-for="item in titleLevel"
                :key="item"
                :label="item"
                :value="item">
            </el-option>
          </el-select>
        </div>

        <div>
            <el-tag>是否启用</el-tag>
          <el-switch
              style="margin-left: 8px"
              v-model="updateJobLevel.enabled"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#c0c0c0">
          </el-switch>
        </div>

      </div>
      <span slot="footer" class="dialog-footer">
    <el-button size="small" @click="dialogVisible = false">取 消</el-button>
    <el-button size="small" type="primary" @click="handleEdit">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "JoblevelMana",
  data() {
    return {
      jobLevel:{
        name:'',
        titleLevel:''
      },
      titleLevel:[
          "正高级",
          "副高级",
          "高级",
          "中级",
          "初级"
      ],
      jobLevels:[],
      multipleSelection: [],
      dialogVisible:false,
      updateJobLevel:{
        name:'',
        titleLevel: "",
        enabled:false
      },
    }
  },
  mounted() {
    this.initJobLevel();
  },
  methods: {

    /**
     *  保存
     */
    handleAdd() {
      if (this.jobLevel.name && this.jobLevel.titleLevel) {
          this.postRequest("/system/basic/joblevel/",this.jobLevel).then(resp => {
            if (resp) {
              this.initJobLevel();
            }
          })
      }else {
        this.$message.error("请将字段填写完整")
      }
    },

    /**
     * 查询
     */
    initJobLevel() {
      this.getRequest("/system/basic/joblevel/").then(resp => {
        if (resp) {
          this.jobLevels = resp;
          this.jobLevel.name = '';
          this.jobLevel.titleLevel = '';
        }
      })
    },

    /**
     * 删除单个
     * @param data
     */
    handleDelete(data) {
      this.$confirm('此操作将永久删除['+ data.name +']职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/joblevel/' + data.id).then(resp => {
          if (resp) {
            this.initJobLevel();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    /**
     * 批量删除
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    handleBatchDelete() {
      this.$confirm('此操作将永久删除['+ this.multipleSelection.length +']条职称信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = "?";
        this.multipleSelection.forEach(item => {
            ids += "ids=" + item.id + "&"
        })
        this.deleteRequest('/system/basic/joblevel/' + ids).then(resp => {
          if (resp) {
            this.initJobLevel();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    /**
     * 编辑
     */
    showDialogView(index, data) {
      Object.assign(this.updateJobLevel,data);
      console.log(this.updateJobLevel)
      this.updateJobLevel.createDate = '';
      this.dialogVisible = true;
    },

    handleEdit() {
        this.putRequest("/system/basic/joblevel/",this.updateJobLevel).then(resp => {
          if (resp) {
            this.initJobLevel();
            this.dialogVisible = false;
          }
        })
    }

  }
}
</script>

<style scoped>
    .updatePosInput {
      margin-bottom: 5px;
      width: 200px;
      margin-left: 5px;
    }
</style>